<?php
// Try extending time, as unziping/ftping took already quite some... :
@set_time_limit( 240 );
defined ( '_JEXEC' ) or die ( 'Restricted access' );

global $jbconfig, $mainframe;
include_once( JPATH_SITE .DS. 'components'.DS.'com_jajobboard'.DS.'helper'.DS.'jafunctions.class.php');
function com_install() {  
  Install_Db();
  
  // Install sub packages
  	global $mainframe;
  	
	$messages = array();
	// Import reqired modules
	jimport( 'joomla.installer.installer' );
	jimport('joomla.installer.helper');
	jimport('joomla.filesystem.file');
	// Get packages
	$p_dir = JRequest::getString('install_directory');
	$p_dir = JPath::clean( JPATH_SITE.DS.'components'.DS.'com_jajobboard'.DS.'packages' );
	$config =& JFactory::getConfig();
	// Did you give us a valid directory?
	if (!is_dir($p_dir)){
		$messages[] = JText::_('Package directory(Related modules, plugins) is missing');
	}
	else 
	{
		$subpackages =JFolder::files($p_dir);
		$config =& JFactory::getConfig();
		$result = true;
		$test = array();
		$installer =new JInstaller();
		if ($subpackages)
		{			
			foreach ($subpackages as $zpackage)
			{
				if( JFile::getExt($p_dir.DS.$zpackage) != "zip" )
					continue;
				$subpackage = JInstallerHelper::unpack($p_dir.DS.$zpackage);
				if ($subpackage)
				{ 
					$type = JInstallerHelper::detectType($subpackage['dir']);
					if (!$type){
						$messages[] = JText::_($zpackage ." Not valid package");
						$result = false;
					}
					if (!$installer->install($subpackage['dir'])){
						// There was an error installing the package
						$messages[] = JText::sprintf('Install %s: %s', $type." ".JFile::getName($zpackage), JText::_('Error'));
					}
					else {
						$messages[] = JText::sprintf('Install %s: %s', $type." ".JFile::getName($zpackage), JText::_('Success'));
					}
					if (!is_file($subpackage['packagefile'])) {
						$subpackage['packagefile'] = $p_dir.DS.$subpackage['packagefile'];
					}
					if (is_dir($subpackage['extractdir'])) {
						JFolder::delete($subpackage['extractdir']);
					}
					if (is_file($subpackage['packagefile'])) {
						JFile::delete($subpackage['packagefile']);
					}
				}
		
			}
		}
	}
  
  # Show installation result to user
  ?>
 <div style="text-align:left;">
  	<table width="100%" border="0" style="line-height:200%; font-weight:bold;">	  
	    <tr>
	      <td align="center">
	      		<img src="../components/com_jajobboard/asset/images/jajobboard.png" />
	      		JA Job Board is installed successfully!<br/>
				<?php echo implode("<br/>", $messages)?><br/><br/>
				<a href="http://wiki.joomlart.com/wiki/JA_Job_Board/Installation_Guides" title="Read more">Read more about: Installation Guides</a>
	      </td>
	    </tr>
    </table>
 </div>
<?php }?>